package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.utils.DateUtil;
import pms.common.utils.PMSLog;
import pms.common.value.ProgressValue;
import pms.common.value.TargetValue;

public class ProgressDaoImpl implements ProgressDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void InsertProgress(ProgressValue value) {
		PMSLog.start();
		String sql="insert into tbl_progress(pgs_target_id,pgs_value,pgs_info,pgs_user_id,pgs_ts) values(?,?,?,?,?)";
		Object[] params = new Object[]{
				value.getPgsTargetId(),
				value.getPgsValue(),
				value.getPgsInfo(),
				value.getCurrentUserId(),
				value.getCurrentDate()
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme update 0");
		} else {
			PMSLog.debug("theme updated");
		}
		PMSLog.end();

	}

	public List<ProgressValue> getProgressList(int targetId) {
		System.out.println("aaaaaaa");
		String sql="select * from tbl_progress where pgs_target_id=? and del_fg='0' order by pgs_id ";
		final List<ProgressValue> l_result = new ArrayList<ProgressValue>();
		jdbcTemplate.query(sql,new Object[]{targetId},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				System.out.println("bbbbb");
				ProgressValue l_tv = new ProgressValue();
				l_tv.setPgsId(rs.getInt("PGS_ID"));
				l_tv.setPgsInfo(rs.getString("PGS_INFO"));
				l_tv.setPgsTargetId(rs.getInt("PGS_TARGET_ID"));
				l_tv.setPgsTs(rs.getString("PGS_TS"));
				l_tv.setPgsValue(rs.getInt("PGS_VALUE"));
				l_tv.setPgsUserId(rs.getInt("PGS_USER_ID"));
				l_result.add(l_tv);
			}			
		});
		return l_result;
	}

}
